package cn.edu.neusoft.yuejian_02.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cn.edu.neusoft.yuejian_02.R;
import cn.edu.neusoft.yuejian_02.bean.UserBean;
import cn.edu.neusoft.yuejian_02.clas.Constants;
import cn.edu.neusoft.yuejian_02.listener.RetrofitListener;
import cn.edu.neusoft.yuejian_02.model.UserModel;
import es.dmoral.toasty.Toasty;


public class LoginActivity extends BaseActivity implements RetrofitListener {


private TextView reg;
    private Button btn_log;
    private  EditText username;
    private  EditText userpass;

    private SharedPreferences sp;
    private String userNameValue,passwordValue;

    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initData();
        initEvents();
       /* HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.i("RetrofitLog","retrofitBack = "+message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);*/



    }




    private void initView() {
        View bg = findViewById(R.id.activity_login);
        bg.getBackground().setAlpha(130);
        View btln = findViewById(R.id.button);
        btln.getBackground().setAlpha(150);




        btn_log = (Button) findViewById(R.id.button);
        username = (EditText) findViewById(R.id.username);
        userpass = (EditText) findViewById(R.id.password);

        //btn_reg = (Button) findViewById(R.id.button2);
        reg = (TextView) findViewById(R.id.reg);


    }

    private void initEvents() {
        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel userModel=new UserModel();
                userNameValue = username.getText().toString();
                passwordValue = userpass.getText().toString();
                userModel.login(username.getText().toString(), userpass.getText().toString(), LoginActivity.this);

            }
        });

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegActivity.class);

                startActivity(intent);
            }
        });


    }

    private void initData() {

    }


    @Override
    public void onSuccess(Object object) {
        UserBean userBean = (UserBean)object;
        String userid = String.valueOf(userBean.getUser_id());

        if("0".equals(userid))
        {
            Toasty.error(LoginActivity.this, "登录失败").show();
        }else
        {

            Toasty.success(LoginActivity.this, "登录成功").show();
            saveUser(userid,userBean.getPhoto());
            Constants.USER_ID = userid;
            Intent intent=new Intent();
            intent.setClass(LoginActivity.this,HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();

        }
    }

    @Override
    public void onFail() {

        Toasty.error(LoginActivity.this, "登录失败").show();
    }
}
